package demo.security;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService;

@Configuration
public class ClientDetailConfiguration {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired	
	private PasswordEncoder passwordEncoder;
	
	@Bean	
	public ClientDetailsService clientDetailsService() {
		
		Map<String,ClientDetails> clientDetailsStore = new HashMap();
		
		BaseClientDetails myclient = new BaseClientDetails("myclient","","openid","authorization_code,refresh_token,client_credentials,password","CLIENT");
		myclient.setClientSecret(passwordEncoder.encode("myclientsecret"));
		clientDetailsStore.put("myclient", myclient);
		
		BaseClientDetails zuulclient = new BaseClientDetails("zuul","","openid","authorization_code,refresh_token,client_credentials,password","CLIENT");
		zuulclient.setClientSecret(passwordEncoder.encode("zuulsecret"));
//		zuulclient.setAutoApproveScopes(Arrays.asList("openid"));
		clientDetailsStore.put("zuul", zuulclient);
		
		BaseClientDetails customerServiceClient = new BaseClientDetails("customer-service-client","","openid","authorization_code,refresh_token,client_credentials,password","CLIENT");
		customerServiceClient.setClientSecret(passwordEncoder.encode("customer-service-secret"));
		clientDetailsStore.put("customer-service-client", customerServiceClient);
		
		BaseClientDetails accountServiceClient = new BaseClientDetails("account-service-client","","openid","authorization_code,refresh_token,client_credentials,password","CLIENT");
		accountServiceClient.setClientSecret(passwordEncoder.encode("account-service-secret"));
		clientDetailsStore.put("account-service-client", accountServiceClient);
				
		InMemoryClientDetailsService clientDetailsService = new InMemoryClientDetailsService();
		clientDetailsService.setClientDetailsStore(clientDetailsStore);
		return clientDetailsService;
	}
}
